package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import main.model.Hierarchy;
import controller.CreateHierarchyController;

public class CreateHierarchyView extends View<CreateHierarchyController> implements Displayable {

	private JFrame frame;
	private JTextField textField;
	private CreateHierarchyController controller;
	private DefaultTableModel entitiesModel;
	private JTable entitiesTable;
	private DefaultTableModel subTableModel;
	private JTable subEntitiesTable;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;

	public CreateHierarchyView() {
		setEntitiesModel(new DefaultTableModel(){
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return true;
			}
		});
		getEntitiesModel().addColumn ("Entidad");
		getEntitiesModel().addColumn ("Cambiar");
		setSubTableModel(new DefaultTableModel(){
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				if (column == 0){
					return false;
				}
				return true;
			}
		});
		getSubTableModel().addColumn ("Entidad");
		getSubTableModel().addColumn ("Borrar");
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initialize() {
		frame = new JFrame("Crear relacion");
		frame.setBounds(100, 100, 607, 578);
		frame.getContentPane().setLayout(null);

		JLabel lblNombre = new JLabel("Nombre");
		lblNombre.setBounds(43, 40, 114, 15);
		frame.getContentPane().add(lblNombre);

		textField = new JTextField();
		textField.setBounds(230, 38, 169, 19);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		setEntitiesTable(new JTable(getEntitiesModel()));
		setSubEntitiesTable(new JTable(getSubTableModel()));
		JButton btnEntidades = new JButton("Agregar entidad");
		btnEntidades.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonAddParentEntity();
			}
		});
		btnEntidades.setBounds(230, 88, 169, 25);
		frame.getContentPane().add(btnEntidades);

		JLabel lblEntidad = new JLabel("Entidad Padre");
		lblEntidad.setBounds(43, 93, 114, 15);
		frame.getContentPane().add(lblEntidad);

		JButton btnTerminar = new JButton("Terminar");
		btnTerminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonTerminar();
			}
		});
		btnTerminar.setBounds(214, 514, 117, 25);
		frame.getContentPane().add(btnTerminar);

		JScrollPane scrollPane = new JScrollPane(getSubEntitiesTable());
		scrollPane.setBounds(43, 284, 462, 104);
		frame.getContentPane().add(scrollPane);

		JScrollPane scrollPane_1 = new JScrollPane(getEntitiesTable());
		scrollPane_1.setBounds(43, 140, 462, 47);
		frame.getContentPane().add(scrollPane_1);
		
		JLabel lblEntidadesHijas = new JLabel("Entidades hijas");
		lblEntidadesHijas.setBounds(46, 233, 146, 29);
		frame.getContentPane().add(lblEntidadesHijas);
		
		JButton button = new JButton("Agregar entidad hija");
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonAddSubEntity();
			}
		});
		button.setBounds(230, 235, 169, 25);
		frame.getContentPane().add(button);
		
		JLabel lblCobertura = new JLabel("Cobertura");
		lblCobertura.setBounds(46, 415, 106, 15);
		frame.getContentPane().add(lblCobertura);
		
		comboBox = new JComboBox();
		comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {Hierarchy.cobTotalExcl, Hierarchy.cobTotalSuper, Hierarchy.cobParcialExcl, Hierarchy.cobParcialSuper}));
		comboBox.setBounds(240, 412, 159, 20);
		frame.getContentPane().add(comboBox);
	}

	public String getName(){
		return this.textField.getText();
	}
	
	public String getTypeSelected(){
		return this.comboBox.getSelectedItem().toString();
	}
	
	@Override
	public void show() {
		frame.setVisible(true);
	}

	@Override
	public void hide() {
		frame.setVisible(false);
	}

	@Override
	public CreateHierarchyController getController() {
		return this.controller;
	}

	@Override
	public void setController(CreateHierarchyController controller) {
		this.controller = controller;
	}

	public JTable getSubEntitiesTable() {
		return subEntitiesTable;
	}

	public void setSubEntitiesTable(JTable subEntitiesTable) {
		this.subEntitiesTable = subEntitiesTable;
	}

	public JTable getEntitiesTable() {
		return entitiesTable;
	}

	public void setEntitiesTable(JTable entitiesTable) {
		this.entitiesTable = entitiesTable;
	}

	public DefaultTableModel getSubTableModel() {
		return subTableModel;
	}

	public void setSubTableModel(DefaultTableModel subTableModel) {
		this.subTableModel = subTableModel;
	}

	public DefaultTableModel getEntitiesModel() {
		return entitiesModel;
	}

	public void setEntitiesModel(DefaultTableModel entitiesModel) {
		this.entitiesModel = entitiesModel;
	}
}
